const { CleanWebpackPlugin } = require("clean-webpack-plugin")
const HtmlWebpackPlugin = require("html-webpack-plugin")
const MiniCssExtractPlugin = require("mini-css-extract-plugin")
const OptimizeCssAssetsWebpackPlugin = require("optimize-css-assets-webpack-plugin")
const TerserWebpackPlugin = require("terser-webpack-plugin")

module.exports = {
    mode: "none",
    entry: {
        main: "./src/main.js",
        home: "./src/home.js"
    },
    output: {
        filename: "[name]-[contenthash:8].bunder.js",
        // path: path.join(__dirname, "dist"),
    },
    optimization: {
    //     usedExports: true,
    //     minimize: true,
    //     concatenateModules: true,
    //     sideEffects: true, 
        minimizer: [
            new TerserWebpackPlugin(),
            new OptimizeCssAssetsWebpackPlugin()
        ],
        splitChunks: {
            chunks: "all" // 标识将所有公共模块提取出来
        }
    },
    module: {
        rules: [
            {
                test: /\.css$/,
                use: [
                    // "style-loader",
                    MiniCssExtractPlugin.loader,
                    "css-loader",
                ]
            },
            {
                test: /\.js$/,
                use: {
                    loader: "babel-loader",
                    options: {
                        presets: ["@babel/preset-env"]
                        // presets: [
                        //     ["@babel/preset-env", {modules: false}]
                        // ]
                    }
                }
            },
        ]
    },
    plugins: [
        new CleanWebpackPlugin(),
        new HtmlWebpackPlugin({
            title: "main",
            template: "./src/main.html",
            filename: "main.html",
            chunks: ["main"]
        }),
        new HtmlWebpackPlugin({
            title: "home",
            template: "./src/home.html",
            filename: "home.html",
            chunks: ["home"]
        }),
        new MiniCssExtractPlugin({
            filename: "[name]-[contenthash:8].bunder.css"
        }),
        // new OptimizeCssAssetsWebpackPlugin()
    ]
}